package edu.zju.gis.dbfg.model.util;

import org.apache.spark.api.java.JavaPairRDD;
import org.locationtech.jts.geom.Geometry;
import scala.Tuple2;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

public class WriteWKT {

    public static void writeToFile(JavaPairRDD<String, GeoItem> rdd, String filename) throws IOException {
        List<Tuple2<String, GeoItem>> featureResult = rdd.collect();
        File featureFile = new File(filename);
        if (featureFile.exists()) featureFile.delete();
        FileWriter featureWriter = new FileWriter(featureFile);
        for (Tuple2<String, GeoItem> feature : featureResult) {
            GeoItem gi = feature._2;
            String tileid=feature._1;
            String id = gi.getFid();
            Double tbmj= Double.valueOf(String.valueOf(gi.getProp().get("TBMJ")));
            //Double tbdlmj= Double.valueOf(String.valueOf(gi.getProp().get("AREA")));
            Geometry geometry = gi.getGeometry();
            String wkt = geometry.toText();
            featureWriter.write(String.format("%s\t%.9f\t%s\t%s\n", id,tbmj, wkt,tileid));
        }
        featureWriter.flush();
        featureWriter.close();
    }
}
